<?php
/**
 * Created by PhpStorm.
 * User: 王文凡<787802868@qq.com>
 * Date: 2016/10/9 0009
 * Time: 11:31
 */

namespace Common\Model;
use Think\Model;

class StudentsInfoModel extends Model
{
    protected $_validate = array(
        array('username','require', '姓名必须填写'),
        array('student_number','require', '学号必须填写'),
        array('student_number','','学号已经存在！',0,'unique',1),
        array('gid','require', '请选择所在年级'),
        array('cid','require', '请选择所在班级'),
        array('guardian','require', '监护人必须填写'),
        array('guardian_tel','require', '监护人电话号码必须填写'),
        array('guardian_tel','/^1[34578][0-9]{9}$/', '手机号格式错误',0,'regex'),
        array('headimg','require', '请上传学生图片'),
        array('idcard','require', '身份证号码必须填写'),
        array('idcard','/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/', '身份证号码格式错误',0,'regex'),
        array('idcard','','身份证号码已经存在！',0,'unique',1),
        array('residence','require', '请填写居住地址')
    );
    //新增学生信息
    public function addStudent($stid=''){
        if(!$this->create()){
            $arr['state']='error';
            $arr['info']=$this->getError();
            return $arr;
        }else{
            $_POST['update_time']=time();
            if(empty($stid)){
                $_POST['create_time']=time();
                if($this->add($_POST)){
                    $arr['state']='success';
                    $arr['info']='添加成功';
                }else{
                    $arr['state']='error';
                    $arr['info']='添加失败';
                }
                return $arr;
            }else{
                if($this->where("stid=%d",$stid)->save($_POST)){
                    $arr['state']='success';
                    $arr['info']='修改成功';
                }else{
                    $arr['state']='error';
                    $arr['info']='修改失败';
                }
                return $arr;
            }
        }
    }
}